﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Kiss.Data;

namespace CsharpTrainer.Core.DAL
{
    public class KissDao<T> : IDAO<T>
    {
        private string _tableName;
        public string TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }

        public string CommandString { get; set; }
        
        public KissDao(string tableName)
        {
            this._tableName = tableName;
        }

        public bool Insert(T obj)
        {
            return false;
        }

        public bool Update(T obj)
        {
            return false;
        }

        public bool Delete(int id)
        {
            return false;
        }

        public T GetSingle(int id)
        {
            using (DbContent db = new DbContent(ApplicationConfig.DbConnection))
            {
                var query = db.DataService.ReadById(_tableName, id);
                return query.ToList<T>(db).FirstOrDefault();
            }
        }

        public IList<T> GetAll()
        {
            return null;
        }

        public IList<T> GetByCriteria(string criteria)
        {
            return null;
        }

        public DataSet GetDsAll()
        {
            return null;
        }

        public DataSet GetDsByCriteria(string criteria)
        {
            return null;
        }

        /*
            Some Extension Methods
         */
        public IList<T> GetByProperty(string property, string value)
        {
            using (DbContent db = new DbContent(ApplicationConfig.DbConnection))
            {
                var query = db.DataService.ReadByColumn(TableName, property, value);
                return query.ToList<T>(db);
            }
        }

        public IList<T> GetByDoubleProperties(string property1, string value1, string property2, string value2)
        {
            using (DbContent db = new DbContent(ApplicationConfig.DbConnection))
            {
                var query = db.DataService.ReadByColumn(TableName, property1, value1, property2, value2);
                return query.ToList<T>(db);
            }
        }

        public IList<T> GetAllBySql(string sql)
        {
            using (DbContent db = new DbContent(ApplicationConfig.DbConnection))
            {
                object[] paras = null;
                var query = db.CommandService.Text(sql, paras);

                return query.ToList<T>(db).ToList<T>();
            }
        }

        public IList<T> GetBySpParam(string sp, Dictionary<string, object> param)
        {
            //using (DbContent db = new DbContent(ApplicationConfig.DbConnection))
            //{
            //    var query = db.CommandService.Procedure("spGetEmployeesByTitle", "Sales Manager");                
            //    return query.ToList<T>(db).ToList<T>();
            //}

            using (DbContent db = new DbContent(ApplicationConfig.DbConnection))
            {
                var query = db.CommandService.Procedure(sp, new Kiss.Core.DataAdapter.DictionaryAdapter(param));

                return query.ToList<T>(db).ToList<T>();
            }
        }
    }
}
